package com.escape.subject.domain.handler.subject;

import com.escape.subject.common.enums.IsDeletedEnum;
import com.escape.subject.common.enums.SubjectInfoTypeEnum;
import com.escape.subject.domain.convert.RadioSubjectConverter;
import com.escape.subject.domain.entity.SubjectAnswerBO;
import com.escape.subject.domain.entity.SubjectInfoBO;
import com.escape.subject.domain.entity.SubjectOptionBO;
import com.escape.subject.infra.basic.entity.SubjectRadio;
import com.escape.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: escape_zebra
 * @CreateTime: 2024-12-26  11:52
 * @Description: 单选题目策略类
 * @Version: 1.0
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getSubjectType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectRadio> radioList = new LinkedList<SubjectRadio>();
        // 1. 校验
        if (subjectInfoBO.getOptionList() == null){
            return;
        }
        // 2. 遍历选项集合
        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE.convertBoToEntity(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
            radioList.add(subjectRadio);

        });
        // 3. 添加集合
        subjectRadioService.batchInsert(radioList);
    }

    /**
     * 题目答案查询
     * @param subjectInfoBO
     * @return
     */
    @Override
    public SubjectOptionBO query(SubjectInfoBO subjectInfoBO) {
        // 1. 查询单选答案集合
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(subjectInfoBO.getId());
        subjectRadio.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        List<SubjectRadio> subjectRadioList = subjectRadioService.queryByCondition(subjectRadio);
        // 2. 转换 entity --> bo
        List<SubjectAnswerBO> subjectAnswerBOList = RadioSubjectConverter.INSTANCE.convertListToBoList(subjectRadioList);

        // 3. 封装
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }


}
